<?php

namespace services\member;

use common\components\Service;
use common\models\member\Attr;

/**
 * Class AttrService
 * @package services\member
 */
class AttrService extends Service
{
    /**
     * 获取属性信息
     *
     * @param array $condition
     * @return Attr|null
     */
    public function findInfo(array $condition = []): ?Attr
    {
        $query = Attr::find();

        if (isset($condition['member_id']) && $condition['member_id']) {
            $query->andWhere(['member_id' => $condition['member_id']]);
        }

        /*if (isset($condition['project_id']) && $condition['project_id']) {
            $andWhere = new Expression(
                "FIND_IN_SET(:project_id, project_id)",
                [":project_id"=> $condition['project_id']]
            );
            $query->andWhere($andWhere);
        }*/

        /*if (isset($condition['clinic_id']) && $condition['clinic_id']) {
            $andWhere = new Expression(
                "FIND_IN_SET(:clinic_id, clinic_id)",
                [":clinic_id"=> $condition['clinic_id']]
            );
            $query->andWhere($andWhere);
        }*/

        if (isset($condition['project_id']) && $condition['project_id']) {
            $query->andWhere(['project_id' => $condition['project_id']]);
        }

        if (isset($condition['department_id']) && $condition['department_id']) {
            $query->andWhere(['department_id' => $condition['department_id']]);
        }

        if (isset($condition['clinic_id']) && $condition['clinic_id']) {
            $query->andWhere(['clinic_id' => $condition['clinic_id']]);
        }

        return $query->one();
    }

    /**
     * 会员属性列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = Attr::find();

        if (isset($condition['member_id'])) {
            $query->andWhere(['member_id' => $condition['member_id']]);
        }

        /*if (isset($condition['project_id']) && $condition['project_id']) {
            $andWhere = new Expression(
                "FIND_IN_SET(:project_id, project_id)",
                [":project_id"=> $condition['project_id']]
            );
            $query->andWhere($andWhere);
        }*/

        if (isset($condition['clinic_id']) && $condition['clinic_id']) {
            $query->andWhere(['clinic_id' => $condition['clinic_id']]);
        }

        if (isset($condition['project_id']) && $condition['project_id']) {
            $query->andWhere(['project_id' => $condition['project_id']]);
        }

        if (isset($condition['department_id']) && $condition['department_id']) {
            $query->andWhere(['department_id' => $condition['department_id']]);
        }

        if (isset($condition['profession_id']) && $condition['profession_id']) {
            $query->andWhere(['profession_id' => $condition['profession_id']]);
        }

        if (isset($condition['is_department_leader']) && $condition['is_department_leader']) {
            $query->andWhere(['is_department_leader' => $condition['is_department_leader']]);
        }

        if (isset($condition['notin_member_id']) && $condition['notin_member_id']) {
            $query->andWhere(['not in', 'member_id', $condition['notin_member_id']]);
        }

        isset($condition['indexBy']) && $query->indexBy($condition['indexBy']);

        return $query->asArray()->all();
    }

    /**
     * 获取会员id
     * @author chenyuebin
     * @param array $params
     * @return \yii\db\ActiveQuery
     */
    public function findMemberId(array $params = [])
    {
        $query = Attr::find()
            ->select('member_id')
            ->where(['project_id' => $params['project_id']]);
        if (isset($params['department_id']) && $params['department_id']) {
            $query->andWhere(['department_id' => $params['department_id']]);
        }
        $data = $query->column();

        return $data;
    }
}
